<?php

jimport('joomla.application.component.controller');

class bgController extends JController {
    public function __construct(){
        parent::__construct();

        $this->model = $this->getModel();
        
    }
    
    /**
     * Remove image when change file
    **/
    public function removeImage(){
        $image_name = JRequest::getVar('image_name');
        if($image_name != null){
            $path = "uploads/images/com_questionpage/$image_name";
            if(file_exists($path)){
                $del = unlink($path);
            }
        }
        
        $app = &JFactory::getApplication();
        $app->close();
    }
    
    
    /* save item questionpage */
    public function save(){
        $data = &JRequest::getVar('jform');
        
        $data['PAGQ_PUBLISHED'] = 1;
        
        /* check addnew or edit */
        if($this->getModel()->checkExistID($data['PAGQ_CODE'])){
            $addNew = false;
        }else{
            $addNew = true;
        }
        if(empty($data['PAGQ_TITLE'])){
                    $error = "ページ名を入力してください。";
                    JRequest::setVar('task','error');
                    JFactory::getApplication()->enqueueMessage($error, 'error');
        }elseif($data['PAGQ_SERQ_CODE'] == "0"){
                    $error = "アンケート名を選択してください。";
                    JRequest::setVar('task','error');
                    JFactory::getApplication()->enqueueMessage($error, 'error');
        }else{ 
            /* get image name */
            if($addNew == true){
                if($data['PAGQ_IMG_NEW'] != null){
                    $data['PAGQ_IMG'] = $data['PAGQ_IMG_NEW'];
                      
                }
            } else {
                if($data['PAGQ_IMG_NEW'] != null){
                        $data['PAGQ_IMG'] = $data['PAGQ_IMG_NEW'];
                        
                } else {
                    if($data['IMG_OLD'] != null){
                        $data['PAGQ_IMG'] = $data['IMG_OLD'];
                    }
                }
            } 
                    
            /* save data, call function save from model */
            if($this->model->save($data, $addNew)){
                //del old file
                if($data['PAGQ_IMG_NEW'] != null){
                    if($data['PAGQ_IMG_NEW'] != $data['IMG_OLD']){
                        $path = "uploads/images/com_questionpage/".$data['IMG_OLD'];
                        if(file_exists($path)){ 
                            $del = unlink($path);
                        }
                    }
                }
                
                $this->setRedirect('index.php?option=com_questionpage');
                   
            } else {
                //JError::raiseError(500, 'Error ! Can\'t save questionpage' );
                //$error = "Error! Can't save!";
                $error = "エラー！データが記録されません。";
                JRequest::setVar('task','');
                JFactory::getApplication()->enqueueMessage($error, 'error');
            }
        }
        parent::display();
    }
    
    /* remove questionpage items */
    public function remove(){
    	$cid = JRequest::getVar('cid');  
        
        /* remove imaage in server */
        foreach($cid as $value){
            //get image name
            $img = $this->getModel()->getImgFromPAGQ_CODE($value);
            //Del Image
            if($img != null){
                
                if($img->PAGQ_IMG != null){
                    $path = "uploads/images/com_questionpage/$img->PAGQ_IMG";
                    if(file_exists($path)){
                        $del = unlink($path);
                    }
                }
                
            }
        }    
        $this->model->remove($cid);
        
        /* REMOVE ANSQ */
        
        if($cid != null){
            $arr_quescode = array();
            for($i = 0; $i < count($cid); $i++){
                $ques_list = $this->model->loadQuesFromPagq($cid[$i]);
                foreach($ques_list as $value){
                    $arr_quescode[] = $value->QUES_CODE;
                }
                
            }
        }
        
        $this->model->removeansq($arr_quescode);
        
        /* remove ques */
        $this->model->removeques($cid);
        
        
        $this->setRedirect('index.php?option=com_questionpage'); 
    }
}